<?php
ob_start();
include("head.php");
include("function.php");
?>

<?php

if(isset($_REQUEST['mode'])&&($_REQUEST['mode']=='changeStatus')) 
{

	changeStatus();
}
elseif(isset($_REQUEST['mode'])&&($_REQUEST['mode']=='delete_userAccount'))
{
	delete_userAccount();
}
elseif(isset($_REQUEST['mode'])&&($_REQUEST['mode']=='payment_userAccount'))
{
	payment_userAccount();
}
elseif(isset($_REQUEST['mode'])&&($_REQUEST['mode']=='active_deactive'))
{
	active_deactive();
}

else
{
	main();
}
?>
<?php

function active_deactive()
{
	 $sql = "UPDATE tblorderdetail SET payment_status = if(payment_status = 'N','Y','N') WHERE order_detail_id = '".$_POST['row_id']."' ";
	mysql_query($sql);
	$GLOBALS['err_msg']=" Status Changed Successfully. ";
	main();
}

function main()
{
	$search_sql ="";
	if (isset($_POST['search_mode']) && ($_REQUEST['search_mode']=="ALPHA"))
	{
				
		//$search_sql .= "SELECT * from shop_user_account as MP, shop_user as US  where MP.user_id = US.user_id and US.user_fname like '".$_POST['txt_alpha']."%'"." ORDER BY US.user_fname";

		$search_sql .= "SELECT * from tblorderdetail as OD, siteusers as SU  where OD.siteuserId = SU.siteuserId and OD.type='db' and OD.payment_status='N' and SU.username like '".$_POST['txt_alpha']."%'"." ";
		
	}
	elseif (isset($_POST['search_mode']) && ($_POST['search_mode']=="SEARCH"))
	{
		if($_POST['search_type']=='first_name')
		{
							
			// $search_sql .= "SELECT * from shop_user_account as MP, shop_user as US  where MP.user_id = US.user_id and US.user_fname like '".$_POST['txt_search']."%'"." ORDER BY US.user_fname";

			$search_sql .= "SELECT * from tblorderdetail as OD, siteusers as SU  where OD.siteuserId = SU.siteuserId and OD.type='db' and OD.payment_status='N' and SU.username like '".$_POST['txt_search']."%'"." ";
		}
	}
	else
	{
	
	 $search_sql .= "SELECT * from tblorderdetail as OD, siteusers as SU  where OD.siteuserId = SU.siteuserId and OD.type='db' and OD.payment_status='N' ";
	
	}
	$total_record = mysql_num_rows(mysql_query($search_sql));
	$limit = 10;
	if(isset($_REQUEST['page']) && ($_REQUEST['page']))
	{
		$start= ($_REQUEST['page'] - 1) * $limit;
		$page = $_REQUEST['page'];
	}
	else
	{
		$start=0;
		$page = 1;
	}
	$search_sql_new = $search_sql." limit $start,$limit";
	//echo $search_sql_new;
	$search_rs = mysql_query($search_sql_new);
	$count = mysql_num_rows($search_rs);
	//echo $search_sql_new;
	//echo $search_sql;
?>
<script>
				function window_open(userId)
					{
						//alert(userId);
				
					var URL = "userdetails.php?userId="+userId;
					window.open(URL,'oprnfile', 'scrollbars=yes,status=no, top=30,left=20,width=502, height=395');
					}
			</script>
<form name = "frmSearch" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<input type="hidden" name="search_mode" value="<?php echo $_POST['search_mode'];?>">
	<input type="hidden" name="txt_alpha" value="<?php echo $_POST['txt_alpha'];?>">
	
  <table width="99%" align="center" border="0" class="BORDER" cellpadding="5" cellspacing="1">
    <tr > 
      <td colspan="2" class="TDHEAD">User Search Panel</td>
    </tr>
    <tr> 
      <td colspan="2" align="center"> 
			<table border="0" cellpadding="0" cellspacing="0" width="80%">
				<tr> 
					<td width="25%" align="right">Search By:</td>
					<td width="75%">
					<select name="search_type">
						<option  value=""  >Select One</option>
						<option value="first_name"  <?php if(isset($_POST['search_type'])&&($_POST['search_type']=="first_name")) echo "selected" ?>>FirstName</option>
					</select>
					<input type="text" name="txt_search" id="txt_search" value="<?php if(isset($_REQUEST['txt_search'])) echo stripslashes($_REQUEST['txt_search']);?>">
					<input type="button" value="Search" onClick="search_text()" class="inplogin">
					<input type="button"  value="Show All" onClick="show_all()" class="inplogin"> 
					</td>
				</tr>
				<tr > 
    			  <td colspan="2">&nbsp;</td>
   				 </tr>
			</table>
		</td>
	</tr>
	
    <tr> 
      <td colspan="2" align="center"> 
        <?php
			if(isset($_POST['txt_alpha']))
			{
				 $str = $_POST['txt_alpha'];
			}
			else
			{
				$str = "1";
			}
			DisplayAlphabet($str); 
		?>
      </td>
    </tr>
  </table>
  <br>
	</form>
	<script language="JavaScript">
	function show_all(){
		document.frmSearch.search_mode.value = "";	
		document.frmSearch.txt_search.value="";
		document.frmSearch.txt_alpha.value="";
		document.frmSearch.search_type.value="";
		document.frmSearch.submit();	
	}
	
	function search_text(){
		//alert(document.frmSearch.search_type.value );
	if(document.frmSearch.search_type.value=="")
	{
	alert("Please Select A Search Type");
	return false;
	}
	if(document.frmSearch.txt_search.value.search(/\S/)==-1)
	{
	alert("Please Enter Search Criteria");
	return false;
	}
		document.frmSearch.search_mode.value = "SEARCH";
		document.frmSearch.submit();
	}
	function search_text1(){
		if(document.frmSearch.user_id.value=="")
		{
		alert("Please Select A User Name");
		return false;
		}
		document.frmSearch.search_mode.value = "SEARCH_FAQ";
		document.frmSearch.submit();
	}
	function search_alpha(alpha){
		document.frmSearch.search_mode.value = "ALPHA";
		document.frmSearch.txt_search.value = '';
		document.frmSearch.txt_alpha.value = alpha;
		document.frmSearch.submit();
	}
	
	
	</script>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="BORDER">

	<tr>
    <td>&nbsp;</td>
  </tr>
	<tr>
    <td height="23" align="center"><font color="Red" id="err_text"><?php echo $GLOBALS['err_msg'];?></font></td>
  </tr>
<tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="23" class="TDHEAD">Manage User Account </td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr><TD>&nbsp;</TD></tr>
<tr> 
  <td colspan="10" id="myTbody-pagination" align="right"></td> 
</tr> 

</table>
	<form name="delete_record" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
		<input type="hidden" name="mode" value="">
	
	<?php if($count==0){?>
	<tr><td height="22" colspan="6" valign="top" align="center">No Record Found</td></tr>
	<?php }else{?>
<table id="myTable" class="tablesorter" cellspacing="1">
<thead>
	<tr align="left" class="TDHEAD_SUB">
	  <th width="8%" height="23" align="center" valign="center" class="text_table_header"><font color="Black">Sl. No.</th>
	  <th width="14%" height="23" align="left" valign="center" class="text_table_header"><font color="Black">User Name</th>
	 <th width="14%" height="23" align="left" valign="center" nowrap="nowrap" class="text_table_header"><font color="Black">Total Ammount&nbsp;&nbsp;&nbsp;</font></th>
	  <th width="15%" height="23" align="center" valign="center" class="text_table_header"><font color="Black">Request for payment</font></th>

	 <th width="15%" height="23" align="center" valign="center" class="text_table_header"><font color="Black">Requested ammount</font></th>

	  <td width="10%" align="center" valign="center" class="text_table_header"><font color="Black"><a href="javascript:payment_state();">Release&nbsp;&nbsp;&nbsp;</a></font></td>

	<td width="12%" height="23" align="center" valign="center" class="text_table_header">Change status</td>

	  <td width="19%" height="23" align="center" valign="center" class="text_table_header">
	<font color="Black"><a href="javascript:delete_userAccount();">Delete</a></font>
	</td>
	 
	</tr>
</thead>
<tbody id="myTbody"> 
	<?php 
		while($faq_row = mysql_fetch_array($search_rs))
		{
			?>
			<tr onMouseOver="this.bgColor='<?=SCROLL_COLOR;?>'" onMouseOut="this.bgColor=''" class="item">
				<td height="22" align="center" valign="top"><?php echo ++$start;?></td>
				<td height="22" align="center" valign="top">
					<?php echo ucfirst(substr(stripslashes($faq_row['username']),0,40));?>
				</td>
				<td height="22" align="center" valign="top">
				<?php
				$query_total_ammount_cr1=mysql_fetch_array(mysql_query("select sum(adspace_price) from tblorderdetail where siteuserId='".$faq_row['siteuserId']."' and type='cr' "));

				$query_total_ammount_cr2=mysql_fetch_array(mysql_query("select sum(adspace_price) from tblorderdetail where siteuserId='".$faq_row['siteuserId']."' and type='db' and payment_status='Y' "));
				echo '$'.$balance=($query_total_ammount_cr1['0']-$query_total_ammount_cr2['0']);
				?>
				</td>
				<td height="22" align="center" valign="top">
				<?php
					if($faq_row['payment_status']=='N')
					{
						
					echo "<font color=Maroon>Requested</font>";
					}
					else
					{
					echo "<font color=Green>Not Requested</font>";
					}
				?>
					
				</td>
				<td height="22" align="center" valign="top">

				<?php
				
				
				if($faq_row['adspace_price']!="")
				{
				echo "<font color=Maroon>"."$". $faq_row['adspace_price']."</font>";
				}
				?>
				</td>
				<td align="center"><?php
				if($faq_row['payment_status']=='N')
					{
					?>	
					<input type="checkbox" value="<?php echo $faq_row['order_detail_id'];?>"  name="allbox1[]" onClick="check(this.value);"/>
					<?php
					}
					?>
				</td>
				
				<td width="13%" align="center" class="text_small" valign="top">
	  <a href="javascript:active_deactive(<?=$faq_row['order_detail_id'];?>,<?=$start?>)" title="Activate Deactivate Status"> 
	  
      <?
			if($faq_row['payment_status']=='N')
			{				
	 ?>
      			
				<font color="#CC0000">Not Paid</font>
      <? 	} 
	  		else
			{ ?>
      			<font color=\"#66CC00\">Paid</font>
      <? 
	  		} 
	  ?>
      </a>
	  </td>

				<td align="center"><input type="checkbox" value="<?php echo $faq_row['order_detail_id'];?>"  name="allbox[]" onClick="check(this.value);"/></td>
				
			</tr>
					 
			<?php	
		}//End of While	 
	?>
</tbody>
	<?php }// End of Count?>
</table>
	</form>
</table>

<form name="frm_opts" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
		<input type="hidden" name="mode" value="">

		<input type="hidden" name="page" value="">
		<input type="hidden" name="status" />
		<input type="hidden" name="account_id" value="">
		<input type="hidden" name="row_id" value="">
		<input type="hidden" name="search_type" value="<?php echo $_POST['search_type'];?>">
		<input type="hidden" name="search_mode" value="<?php $_POST['search_mode'];?>">
		<input type="hidden" name="txt_alpha" value="<?php $_POST['txt_alpha'];?>">
		<input type="hidden" name="txt_search" value="<?php $_POST['txt_search'];?>">
</form>
<script language="JavaScript">
	function checkAll(){
	var arr = new Array();
	for (var i=0;i<document.delete_record.elements.length;i++)
	{
		var e=document.delete_record.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
			e.checked=document.delete_record.allb.checked;
		}
	}
	
	}
	
	function check(id)
	{	
//alert(id);
		var ele=document.delete_record.elements["allbox[]"];
		//alert(ele.length);
		var chk=0;
		for(var i=0;i<ele.length;i++)
		{
			if(ele[i].checked==true)
			chk=chk+1;
		}
		if(chk==ele.length)document.delete_record.allb.checked="true";
		else document.delete_record.allb.checked="";
	
	}


	function delete_userAccount()
		{
			var FAQResp = window.confirm("Are you sure to remove the selected User?");
			if( FAQResp == true )
			{
				document.delete_record.mode.value='delete_userAccount';
				document.delete_record.submit();
			}
		}
	function payment_state()
		{
			var FAQResp = window.confirm("Are you sure to give payment to the selected User?");
			if( FAQResp == true )
			{
				//document.delete_record.mode.value='payment_userAccount'; err_text
				document.getElementById('err_text').innerHTML="please change status after payment through paypal.";
				document.delete_record.action="mass.php"
				document.delete_record.submit();
			}
		}
	</script>
<script language="javascript">
	function add_company()
	{
		frm = document.frm_opts;
		frm.mode.value = 'add_company';
		frm.submit();
	}

	function active_deactive(ID,record_no)
		{
			document.frm_opts.mode.value='active_deactive';
			document.frm_opts.row_id.value=ID;
			//document.frm_opts.hold_page.value = record_no*1;
			document.frm_opts.submit();
		}

	function edit_company(make_id)
	{
		//alert(make_id);
		frm = document.frm_opts;
		frm.make_id.value = make_id;
		frm.mode.value = 'edit_company';
		
		frm.submit();
	}
function changeStatus(user_id,status)
		{
			var CountryResp = window.confirm("Are you sure want to change status?");
			if( CountryResp == true )
			{
				frm= document.frm_opts;
				frm.user_id.value = user_id;
				frm.status.value = status;
				frm.mode.value = 'changeStatus';
				frm.submit();
			}
		}
</script>
<?php
} 
/**
/*  ENDS Of main function 
**/
/**
/*  Function Add FAQ - STARTS
**/
function add_company()
{
	?>
<script language="javascript">
/***************************** START OF VALIDATION *************************************************/
		function chk_addFAQ(frm)
		{
			if(frm.name.value.search(/\S/) == -1)
			{
				alert("Please! Enter ScoreName.");
				frm.name.focus();
				return false;
			}
	frm.mode.value = 'insert_company';
		}
/***************************** END OF VALIDATION *************************************************/

	</script>
	<form name="frm_addFAQ" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" onSubmit="return chk_addFAQ(this);">
	<input type="text" name="mode" value="">
		<table align="center" width="99%" cellpadding="2" cellspacing="0" class="BORDER">
			<tr>
				<td align="center" valign="top" colspan="3" class="TDHEAD">Add Company</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td align="right" valign="top" width="40%" style="padding-left:15px;">Company Name</td>
				<td align="center" valign="top" width="5%">:</td>
				<td align="left" valign="top"><input type="text" name="name"  ></td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td align="right" valign="top" width="40%" style="padding-left:15px;">Is Active?</td>
				<td align="center" valign="top" width="5%">:</td>
				<td align="left" valign="top">
					<input type="radio" name="status" value="Y" checked="checked">&nbsp;Active
					<input type="radio" name="status" value="N">&nbsp;In-Active
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td align="left" colspan="2" valign="top" width="40%" style="padding-left:15px;">&nbsp;</td>
				<td align="left" valign="top"><input type="submit" value="Submit" id="btnsubmit">&nbsp;<input type="button" value="Cancel" onClick="document.frm_addFAQ.submit();"></td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			
		</table>
	</form>
	<?php
}
/**
/*  Function Add FAQ - ENDS
**/
/**
/*  Function Insert FAQ - STARTS
**/
function changeStatus()
{
	if(isset($_REQUEST['status'])&&($_REQUEST['status']=='Y'))
	{
		$updatestatusSQL = "UPDATE shop_user_account SET status = 'N' WHERE user_id = '".$_REQUEST['user_id']."'";
		//ECHO $updatestatusSQL; 
	}
	elseif(isset($_REQUEST['status'])&&($_REQUEST['status']=='N'))
	{
		$updatestatusSQL = "UPDATE shop_user_account SET status = 'Y' WHERE user_id = '".$_REQUEST['user_id']."'";
	}
	//echo $updatestatusSQL; 
	mysql_query($updatestatusSQL);
	if(mysql_affected_rows()==1)
	{
		$GLOBALS['err_msg'] = "Profile Status Updated Successfully";
	}
	else
	{
		$GLOBALS['err_msg']  = "Failed To Update Profile Status";
	}
	?>
	<form name="frmchng" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
	<input type="hidden" name="make_id" value="<?php echo $_REQUEST['make_id'];?>" />
	
	</form>
	<script language="javascript">
	document.frmchng.submit();
	</script>
	<?php 
}
function insert_company()
{
	$chk_cat_rs = mysql_fetch_array(mysql_query("SELECT count(make_id) FROM ".CARMAKE." WHERE  make_name= '".mysql_escape_string($_REQUEST['name'])."'"));
	if($chk_cat_rs[0]>0)
	{
		$GLOBALS['err_msg'] = "Duplicate Company Information";
	}
	else
	{
		$insert_faq_sql = "INSERT INTO ".CARMAKE." SET ".
						" make_name  	 = '".$_REQUEST['name']."'".
					
						" ,status             = '".$_REQUEST['status']."'";
		//echo $insert_faq_sql;
		if(mysql_query($insert_faq_sql))
		{
			$GLOBALS['err_msg'] = "Data Entered Successfully";
		}
		else
		{
			$GLOBALS['err_msg'] = "Failed To insert Data. Please! Try Again.";
		}
	}
	main();
	exit();
}
/**
/*  Function Insert FAQ - ENDS
**/

/**
/*  Function Edit FAQ - STARTS
**/
function edit_company()
{
$faq_row = mysql_fetch_array(mysql_query("SELECT * FROM ".CARMAKE." WHERE make_id = '".$_REQUEST['make_id']."'"));

	?>
<script language="javascript">
/***************************** START OF VALIDATION *************************************************/
		function chk_editFAQ(frm)
		{
			if(frm.name.value.search(/\S/) == -1)
			{
				alert("Please! Enter Scorename.");
				frm.name.focus();
				return false;
			}
// 			if(frm.point.value.search(/\S/) == -1)
// 			{
// 				alert("Please! Enter Score Point.");
// 				frm.point.focus();
// 				return false;
// 			}
// 			var val=frm.point.value;
// 			if(isNaN(val))
// 			{
// 				alert("Score Point Must Be Numeric.");
// 				frm.point.focus();
// 				return false;
// 			}
			frm.mode.value = 'update_company';
		}
/***************************** END OF VALIDATION *************************************************/

	</script>
	<form name="frm_editFAQ" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" onSubmit="return chk_editFAQ(this);">
	<input type="hidden" name="mode">
	<input type="hidden" name="make_id" value="<?php echo $_REQUEST['make_id'];?>">
		<table align="center" width="99%" cellpadding="2" cellspacing="0" class="BORDER">
			<tr>
				<td align="center" valign="top" colspan="3" class="TDHEAD">Edit Company</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td align="right" valign="top" width="40%" style="padding-left:15px;">Company Name</td>
				<td align="center" valign="top" width="5%">:</td>
				<td align="left" valign="top"><input type="text" name="name"  value="<?php echo stripslashes($faq_row['make_name']);?>"></td>
			</tr>
			
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td align="right" valign="top" width="40%" style="padding-left:15px;">Is Active?</td>
				<td align="center" valign="top" width="5%">:</td>
				<td align="left" valign="top">
					<input type="radio" name="status" value="Y" <?php if(isset($faq_row['status'])&&($faq_row['status']=='Y'))echo 'checked';?>>&nbsp;Active
					<input type="radio" name="status" value="N" <?php if(isset($faq_row['status'])&&($faq_row['status']=='N'))echo 'checked';?>>&nbsp;In-Active
				</td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			<tr>
				<td align="left" colspan="2" valign="top" width="40%" style="padding-left:15px;">&nbsp;</td>
				<td align="left" valign="top"><input type="submit" value="Submit">&nbsp;<input type="button" value="Cancel" onClick="document.frm_editFAQ.submit();"></td>
			</tr>
			<tr><td colspan="3">&nbsp;</td></tr>
			
		</table>
	</form>
	<?php
}
/**
/*  Function Edit FAQ - ENDS
**/

/**
*	Function Update FAQ - STARTS
**/
function update_company()
{
		$chk_cat_rs = mysql_fetch_array(mysql_query("SELECT count(make_id) FROM ".CARMAKE." WHERE make_name = '".mysql_escape_string($_REQUEST['name'])."' AND make_id<> '".$_REQUEST['make_id']."'"));
	if($chk_cat_rs[0]>0)
	{
		$GLOBALS['err_msg'] = "Duplicate Company Information";
	}
	else
	{
		$insert_faq_sql = "UPDATE ".CARMAKE." SET ".
						" make_name          = '".$_REQUEST['name']."'".
					
						" ,status             = '".$_REQUEST['status']."' WHERE make_id = '".$_REQUEST['make_id']."'";
		//echo $insert_faq_sql;
		if(mysql_query($insert_faq_sql))
		{
			$GLOBALS['err_msg'] = "Data Updated Successfully";
		}
		else
		{
			$GLOBALS['err_msg'] = "Failed To Update Data. Please! Try Again.";
		}
	}
	main();
	exit();

}
/**
*	Function Update FAQ - ENDS
**/
 
/**
*	Function to delete FAQ - STARTS
**/
function delete_userAccount()
{
	if(isset($_REQUEST['allbox']))
	{
		foreach($_REQUEST['allbox'] as $userId)
		{
		    $sql_query="DELETE FROM tblorderdetail WHERE  order_detail_id='".$userId."'";
			mysql_query($sql_query);
			if(mysql_affected_rows()==1)
				$GLOBALS['err_msg']="Information Deleted Successfully.";
			else
				$GLOBALS['err_msg']="Failed To Delete Information.";
		}
	}
	else
	{
		$GLOBALS['err_msg']="No Record Selected.";
	}
	main();
	exit();
}
/*
	Function to delete FAQ - ENDS
*/
// function payment_userAccount()
// {
// 	if(isset($_REQUEST['allbox1']))
// 	{
// 		foreach($_REQUEST['allbox1'] as $userId)
// 		{
// 				$total_credit=mysql_fetch_array(mysql_query("select  sum(ammount) as total from shop_account_details where user_id='".$userId."' and details='credited' and status='B' "));
// 				$emailid=mysql_fetch_array(mysql_query("select user_email,user_id from shop_user where user_id='".$userId."' "));
// 				
// 				 $value3=$value1-$value2;
// 				 $value=number_format($total_credit[0], 2, '.', '');			  									
// 				if ((!isset($value)) OR ($value == ""))
// 				 {
// 					$value = "\t";
// 				 }
// 				 
// 				 
// 				 
// 				  else 
// 				 {
// 					$value = str_replace('"', '""', $value); 
// 					$value = '"' . $value . '"' . "\t";
// 				 }
// 				 
// 				 
// 				$line.=$emailid['user_email']."\t";
// 				$line .= $value;
// 				
// 			
// 
// 		}
// 		$line .= 'USD' . "\t";
// 		
// 	        $data .= trim($line)."\n";
// 		if ($data == "")
// 		{
// 		$data = "\n(0) Records Found!\n";						
// 		}
// 		else
// 		{
// 		header("Content-type: application/octet-stream");
// 		header("Content-Disposition: attachment; filename=test.xls");
// 		header("Pragma: no-cache");
// 		header("Expires: 0");
// 		print $data;
// 		}
// 	}
// 	
// 	else
// 	{
// 		$GLOBALS['err_msg']="No Record Selected.";
// 	}
// 
// 	
// 
// 	main();
// 	exit();
// }
include("foot.php") ;

?>
